import 'package:banyou_app/app/config/colors.dart';
import 'package:banyou_app/app/widget/load_network_image_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';

import '../controllers/home_controller.dart';

class HomeView extends GetView<HomeController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Positioned.fill(
              child: Image.asset(
            'assets/images/背景@2x.png',
            width: 1.sw,
            height: 1.sh,
            fit: BoxFit.cover,
          )),
          Positioned.fill(
            child: SafeArea(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Obx(() => controller.showAddress.value
                        ? addressWidget()
                        : Container()),
                    SizedBox(height: 10),
                    titleWidget(),
                    SizedBox(height: 10),
                    Obx(() => controller.index == 0
                        ? Container(
                            padding: EdgeInsets.symmetric(horizontal: 16),
                            child: Obx(() => ListView.separated(
                                padding: EdgeInsets.zero,
                                shrinkWrap: true,
                                physics: NeverScrollableScrollPhysics(),
                                itemBuilder: (c, i) {
                                  Map aaa = controller.dataList[i];
                                  return itemWidget(aaa, 0);
                                },
                                separatorBuilder: (c, i) {
                                  return Container(
                                    height: 10,
                                  );
                                },
                                itemCount: controller.dataList.length)),
                          )
                        : Container(
                            padding: EdgeInsets.symmetric(horizontal: 16),
                            child: Obx(() => ListView.separated(
                                padding: EdgeInsets.zero,
                                shrinkWrap: true,
                                physics: NeverScrollableScrollPhysics(),
                                itemBuilder: (c, i) {
                                  Map aaa = controller.dataList1[i];
                                  return itemWidget(aaa, 1);
                                },
                                separatorBuilder: (c, i) {
                                  return Container(
                                    height: 10,
                                  );
                                },
                                itemCount: controller.dataList1.length)),
                          ))
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget itemWidget(item, type) {
    List expectations = item['Labels'] ?? [];
    return InkWell(
      onTap: () {
        controller.toInfo(item);
      },
      child: Container(
        width: 1.sw,
        clipBehavior: Clip.hardEdge,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(10.r)),
        ),
        child: AspectRatio(
          aspectRatio: 1,
          child: Stack(
            children: [
              Positioned.fill(
                  child: LoadNetworkImageWidget(
                      width: 1.sw,
                      height: 1.sw,
                      fit: BoxFit.cover,
                      imageUrl: item['UserAvatar'] ?? "")),
              Positioned.fill(
                  child: Column(
                children: [
                  Spacer(),
                  Container(
                    width: 1.sw,
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: [
                            Color(0x00000000).withAlpha(50),
                            Color(0xFF000000).withAlpha(50),
                          ]),
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 16, vertical: 4),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Text(item['UserName'] ?? "",
                                style: TextStyle(
                                    fontSize: 18.sp, color: ColorConfig.white)),
                            SizedBox(width: 10),
                            item['UserGender'] == '女'
                                ? Image.asset(
                                    'assets/images/女@2x.png',
                                    width: 18,
                                  )
                                : Image.asset(
                                    'assets/images/nan.png',
                                    width: 20,
                                  ),
                          ],
                        ),
                        Text(
                            '${item['UserAge'] != null ? item['UserAge'].toString() + "岁" : ""}/${item['UserWeight'] ?? ""}',
                            style: TextStyle(
                                fontSize: 14.sp, color: ColorConfig.white)),
                        SizedBox(height: 10),
                        Container(
                          child: Wrap(
                            children: expectations
                                .map((item) => typeWidget(item))
                                .toList(),
                            spacing: 10,
                            runSpacing: 15,
                            alignment: WrapAlignment.start,
                            runAlignment: WrapAlignment.end,
                          ),
                        ),
                        SizedBox(height: 10),
                      ],
                    ),
                  )
                ],
              )),
              type == 1
                  ? Positioned(
                      top: 12.h,
                      left: 0,
                      child: Container(
                          padding:
                              EdgeInsets.symmetric(vertical: 4, horizontal: 6),
                          decoration: BoxDecoration(
                            gradient: LinearGradient(
                                begin: Alignment.centerLeft,
                                end: Alignment.centerRight,
                                colors: [
                                  Color(0xFF755DF3),
                                  Color(0xFFDF7EC0),
                                ]),
                            borderRadius: BorderRadius.only(
                                topRight: Radius.circular(10)),
                          ),
                          child: Row(
                            children: [
                              Image.asset('assets/images/距离@2x.png',
                                  width: 12.sp),
                              SizedBox(width: 3),
                              Text(item['Distance'] ?? "",
                                  style: TextStyle(
                                      fontSize: 10.sp,
                                      color: ColorConfig.white)),
                            ],
                          )))
                  : Container()
            ],
          ),
        ),
      ),
    );
  }

  Widget typeWidget(item) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 5, vertical: 2),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(10.r)),
        gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              Color(0x00000000).withAlpha(50),
              Color(0xFF000000).withAlpha(50),
            ]),
      ),
      child: Text(item,
          style: TextStyle(color: ColorConfig.white, fontSize: 14.sp)),
    );
  }

  Widget titleWidget() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16),
      child: Row(
        children: [
          Obx(() => InkWell(
                onTap: () {
                  controller.clickIndex(0);
                },
                child: Text(
                  '推荐',
                  style: TextStyle(
                      fontSize: controller.index == 0 ? 24.sp : 20.sp,
                      fontWeight: FontWeight.bold,
                      color: controller.index == 0
                          ? ColorConfig.text_label
                          : ColorConfig.subtitle_text),
                ),
              )),
          SizedBox(width: 20),
          Obx(() => InkWell(
                onTap: () {
                  controller.clickIndex(1);
                },
                child: Text(
                  '附近',
                  style: TextStyle(
                      fontSize: controller.index == 1 ? 24.sp : 20.sp,
                      fontWeight: FontWeight.bold,
                      color: controller.index == 1
                          ? ColorConfig.text_label
                          : ColorConfig.subtitle_text),
                ),
              )),
          // Spacer(),
          // InkWell(
          //     onTap: () {
          //       controller.shaiXuan();
          //     },
          //     child: Row(
          //       children: [
          //         Text('筛选',
          //             style:
          //                 TextStyle(fontSize: 14.sp, color: Color(0xFF959595))),
          //         Image.asset(
          //           'assets/images/筛选@2x.png',
          //           width: 18,
          //         ),
          //       ],
          //     ))
        ],
      ),
    );
  }

  Widget addressWidget() {
    return InkWell(
      onTap: () {
        controller.openSet();
      },
      child: Container(
        margin: EdgeInsets.only(left: 16, right: 16, top: 15),
        padding: EdgeInsets.symmetric(),
        width: 1.sw,
        height: 45,
        child: Stack(
          children: [
            Positioned.fill(
                child: Image.asset(
              'assets/images/dingwei.png',
              width: 1.sw,
              fit: BoxFit.fill,
            )),
            Positioned.fill(
              child: Row(
                children: [
                  SizedBox(width: 55),
                  Text('开启位置权限',
                      style: TextStyle(
                        fontSize: 14,
                        color: ColorConfig.text_label,
                      )),
                  Spacer(),
                  Icon(
                    Icons.chevron_right_rounded,
                    size: 18,
                    color: ColorConfig.subtitle_color,
                  ),
                  SizedBox(width: 15)
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
